package com.magus.project.yddemo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magus.framework.core.persistence.JpaSearchUtils;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.repository.BaseRepository;
import com.magus.framework.core.service.BaseService;
import com.magus.framework.system.entity.Company;
import com.magus.project.yddemo.entity.Indicator;
import com.magus.project.yddemo.entity.IndicatorData;
import com.magus.project.yddemo.entity.IndicatorDetail;
import com.magus.project.yddemo.repository.IndicatorDataRepository;

/**
 * 指标 Service
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Service
@Transactional(readOnly = true)
public class IndicatorDataService extends BaseService<IndicatorData, String> {

	@Autowired
	private IndicatorDataRepository indicatorDataRepository;

	@Autowired
	private IndicatorService indicatorService;

	@Override
	protected BaseRepository<IndicatorData, String> getBaseRepository() {
		return this.indicatorDataRepository;
	}

	// 集团公司燃煤机组环保指标月评价结果
	public List<Indicator> getIndicatorList() {
		List<SearchFilter> searchFilters = new ArrayList<>();
//		searchFilters.add(SearchFilter.like("major.name", "环保"));
		searchFilters.add(SearchFilter.like("dimension", "month"));
		searchFilters.add(SearchFilter.equal("group.name", "燃煤机组环保指标"));

		Specification<Indicator> spec = JpaSearchUtils.buildAndSpec(searchFilters);

		return indicatorService.findAll(spec);
	}

	public List<IndicatorData> getIndicatorDataList(List<Indicator> indicatorList, List<Company> companyList,
			List<IndicatorData> dataList) {
		List<IndicatorData> result = new ArrayList<IndicatorData>();
		Map<String, IndicatorData> dataMap = this.getMap(dataList);

		for (Company company : companyList) {
			for (Indicator indicator : indicatorList) {

				String idStr = company.getId() + ";" + indicator.getId();
				if (dataMap.containsKey(idStr)) {
					result.add(dataMap.get(idStr));
				} else {
					IndicatorData indiData = new IndicatorData();

					IndicatorDetail detail = new IndicatorDetail();
					detail.setIndicator(indicator);
					detail.setCompany(company);

					indiData.setIndicatorDetail(detail);
					indiData.setDimension("month");

					result.add(indiData);
				}

			}
		}

		return result;
	}

	public Map<String, IndicatorData> getMap(List<IndicatorData> dataList) {
		Map<String, IndicatorData> map = new HashMap<String, IndicatorData>();

		for (IndicatorData data : dataList) {
			IndicatorDetail indicatorDetail = data.getIndicatorDetail();
			Company gs = indicatorDetail.getCompany();
			Indicator indi = indicatorDetail.getIndicator();
			String companyIndicatorIdStr = gs.getId() + ";" + indi.getId();
			if (map.containsKey(companyIndicatorIdStr)) {
			} else {
				map.put(companyIndicatorIdStr, data);
			}
		}
		return map;
	}

	public List<IndicatorData> findByIndicatorDetailIn(List<IndicatorDetail> details) {
		return indicatorDataRepository.findByIndicatorDetailIn(details);
	}

	public List<IndicatorData> findByIndicatorDetailIn(List<IndicatorDetail> details, Long beginTime, String string) {
		if(details==null) {
			details=new ArrayList<>();
		}
		return indicatorDataRepository.findByIndicatorDetailInAndTimeAndDimension(details,beginTime,string);
	}
	

}
